import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from agent import WindowAgent
import time

def main():
    # 创建 WindowAgent 实例
    agent = WindowAgent()
    
    print("=== WindowAgent 示例程序 ===")
    print(f"当前活动窗口: {agent.get_active_window_title()}\n")
    
    # 示例1: 直接启动记事本
    print("启动记事本...")
    if agent.start_app("notepad.exe"):
        print("等待记事本窗口...")
        if agent.wait_for_window("记事本", timeout=5):
            print("记事本已打开")
            time.sleep(1)
            
            # 验证当前窗口
            active_window = agent.get_active_window_title()
            print(f"当前活动窗口: {active_window}")
            
            if active_window and "记事本" in active_window:
                print("输入文本...")
                agent.type_text("Hello, World!", interval=0.05)
            else:
                print("警告: 当前窗口不是记事本，跳过输入")
    
    # 示例2: 发送快捷键（确保在记事本中）
    if agent.get_active_window_title() and "记事本" in agent.get_active_window_title():
        print("发送 Ctrl+A 选择全部...")
        agent.send_keys(['ctrl', 'a'])
        time.sleep(0.5)
    
    # 示例3: 截屏
    print("\n截屏...")
    screenshot = agent.screenshot("screenshot.png")
    if screenshot:
        print("截屏保存成功")
    
    # 示例4: 运行程序
    print("\n启动计算器...")
    agent.run_program("calc.exe")
    time.sleep(2)
    
    # 示例5: 获取窗口列表
    print("\n获取当前窗口列表...")
    windows = agent.get_window_list()
    print(f"当前打开的窗口: {windows}")
    
    # 示例6: 聚焦到特定窗口
    print("\n聚焦到计算器...")
    if agent.focus_window("计算器"):
        print(f"当前活动窗口: {agent.get_active_window_title()}")

if __name__ == "__main__":
    main()